package com.dhcc.cdc.busi.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcLocationEntity;
import com.dhcc.cdc.busi.service.ICdcLocationService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.entity.Dept;
import com.dhcc.core.modules.system.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 科室病区
 * 
 * @ClassName: CdcLocationController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:07
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdclocation")
public class CdcLocationController extends BaseController {

	private String PREFIX = "/backend/cdc/cdclocation/";

	@Autowired
	private ICdcLocationService cdcLocationService;
    @Autowired
    private IDeptService deptService;
    /**
     * 跳转到科室病区列表首页
     */
    @RequestMapping("")
    @Permission("cdclocation:list")
    public String index() {
        return PREFIX + "cdclocation.html";
    }
	
    /**
     * 跳转到新增科室病区
     */
    @RequestMapping("/to_add")
    @Permission("cdclocation:add")
    public String toAdd() {
        return PREFIX + "cdclocation_add.html";
    }
	
	/**
     * 跳转到修改科室病区
     */
    @RequestMapping("/to_update/{cdcLocationId}")
    @Permission("cdclocation:update")
    public String toUpdate(@PathVariable Long cdcLocationId, Model model) {
        CdcLocationEntity cdcLocation = this.cdcLocationService.selectById(cdcLocationId);
        if(CommonUtil.isNotEmpty(cdcLocation.getSysDeptId())){
            Dept dept = this.deptService.selectById(cdcLocation.getSysDeptId());
            cdcLocation.setSysDept(dept);
        }
        if(CommonUtil.isNotEmpty(cdcLocation.getParLocId())){
            CdcLocationEntity parLoc = this.cdcLocationService.selectById(cdcLocation.getParLocId());
            cdcLocation.setParLoc(parLoc);
        }
        model.addAttribute("cdcLocation",cdcLocation);
        return PREFIX + "cdclocation_edit.html";
    }

    /**
     * 获取科室病区列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdclocation:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcLocationEntity> page = new PageFactory<CdcLocationEntity>().defaultPage();
        List<CdcLocationEntity> result = this.cdcLocationService.page(page,params);
        page.setRecords((List<CdcLocationEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }
    
    @RequestMapping(value = "/query/{hospID}")
    @ResponseBody
    @Permission("cdclocation:list")
    public Object query(@PathVariable(required = false) Long hospID,@RequestParam Map<String, Object> params) {
        if(params.containsKey("q")) params.put("description", params.get("q"));
        if(CommonUtil.isNotEmpty(hospID)) params.put("hospitalId", hospID);
        List<CdcLocationEntity> result = this.cdcLocationService.list(params);
        return result;
    }


    /**
     * 新增科室病区
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdclocation:add")
    @BussinessLog(value = "新增科室病区")
    public Object add(CdcLocationEntity cdcLocation) {
        if (CommonUtil.isOneEmpty(cdcLocation)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcLocation.insert();
        return R.ok();
    }

    /**
     * 删除科室病区
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdclocation:delete")
    @BussinessLog(value = "删除科室病区")
    public Object delete(@RequestParam Long cdcLocationId) {
	
        this.cdcLocationService.deleteById(cdcLocationId);

        return R.ok();
    }

    /**
     * 修改科室病区
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdclocation:update")
    @BussinessLog(value = "修改科室病区")
    public Object update(CdcLocationEntity cdcLocation) {
        if (CommonUtil.isOneEmpty(cdcLocation)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcLocationEntity old = this.cdcLocationService.selectById(cdcLocation.getId());

        old.setXcode(cdcLocation.getXcode());
        old.setCode(cdcLocation.getCode());
        old.setDescription(cdcLocation.getDescription());
        old.setDescription2(cdcLocation.getDescription2());
        old.setBusLocTypeId(cdcLocation.getBusLocTypeId());
        old.setBusLocRoleId(cdcLocation.getBusLocRoleId());
        old.setIsOper(cdcLocation.getIsOper());
        old.setIsNicu(cdcLocation.getIsNicu());
        old.setIsIcu(cdcLocation.getIsIcu());
        old.setBusLocIcuId(cdcLocation.getBusLocIcuId());
        old.setParLocId(cdcLocation.getParLocId());
        old.setHospitalId(cdcLocation.getHospitalId());
        old.setDataId(cdcLocation.getDataId());
        old.setIsActive(cdcLocation.getIsActive());
        old.setSysDeptId(cdcLocation.getSysDeptId());
        old.updateAllColumnById();
        return R.ok();
    }

    /**
     * @Description: 根据医院查询科室
     * @Title: listLocationByHospID
     * @return
     * @return: Object
     * @throws
     * @author: zhangdc
     * @date:2021-06-27 10:09:15
     */
    @RequestMapping(value = "/listLocationByHospID")
    @ResponseBody
    public Object listLocationByHospID(@RequestParam Long hospID) {
        Map<String, Object> map = new HashMap<>();
        map.put("hospitalId", hospID);
        List<CdcLocationEntity> result = this.cdcLocationService.list(map);
        JSONArray array = new JSONArray();
        JSONObject obj = new JSONObject();
        obj.put("value", "");
        obj.put("text","全部");
        array.add(obj);
        for(CdcLocationEntity depart : result){
            obj = new JSONObject();
            obj.put("value", depart.getId() + "");
            obj.put("text",depart.getDescription());
            array.add(obj);
        }

        return R.ok(array.toJSONString());
    }
	
}
